package com.sg.service.base.dataassets.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.dataassets.req.QueryAlbumFileRelationDetailReq;
import com.sg.dto.base.dataassets.req.QueryAlbumFileRelationListReq;
import com.sg.entity.OmsAlbumFileRelation;
import com.sg.service.base.dataassets.MOmsAlbumFileRelationService;
import com.wicket.okrdataassets.common.dto.FieldInfo;
import com.wicket.okrdataassets.common.exception.BizException;
import com.wicket.okrdataassets.dal.mapper.mbg.OmsAlbumFileRelationMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAlbumFileRelationServiceImpl
        extends ServiceImpl<OmsAlbumFileRelationMapper, OmsAlbumFileRelation>
        implements MOmsAlbumFileRelationService {

    @Autowired
    OmsAlbumFileRelationMapper omsAlbumFileRelationMapper;

    /**
     * 8-02-02新增专辑文件关系[10312]
     * gen by moon at 11/21/2024, 9:37:51 PM
     */
    @Trace(operationName = "8-02-02新增专辑文件关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAlbumFileRelation(OmsAlbumFileRelation omsAlbumFileRelation) {
        omsAlbumFileRelation.setAlbumRelationId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAlbumFileRelation.setIsArchive("FALSE");
        omsAlbumFileRelation.setSubjectLifeCycle("CURRENTLY_VALID");
        omsAlbumFileRelation.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAlbumFileRelation.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAlbumFileRelation != null && omsAlbumFileRelation.getSubjectLifeCycle() != null && omsAlbumFileRelation.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAlbumFileRelation.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAlbumFileRelation != null && omsAlbumFileRelation.getSubjectLifeCycle() != null && omsAlbumFileRelation.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAlbumFileRelation.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAlbumFileRelation.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAlbumFileRelation.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAlbumFileRelation != null && omsAlbumFileRelation.getSubjectLifeCycle() != null && omsAlbumFileRelation.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAlbumFileRelation.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAlbumFileRelation != null && omsAlbumFileRelation.getSubjectLifeCycle() != null && omsAlbumFileRelation.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAlbumFileRelation.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        omsAlbumFileRelation.setIsValid("FALSE");
        omsAlbumFileRelation.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsAlbumFileRelation.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsAlbumFileRelation)) {
            return omsAlbumFileRelation.getAlbumRelationId();
        } else {
            return "-1";
        }

    }

    /**
     * 8-02-02修改专辑文件关系[10313]
     * gen by moon at 11/16/2024, 3:32:48 PM
     */
    @Trace(operationName = "8-02-02修改专辑文件关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAlbumFileRelation(OmsAlbumFileRelation omsAlbumFileRelation) {
        omsAlbumFileRelation.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAlbumFileRelation.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAlbumFileRelation != null && omsAlbumFileRelation.getSubjectLifeCycle() != null && omsAlbumFileRelation.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAlbumFileRelation.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        if (omsAlbumFileRelation != null && omsAlbumFileRelation.getSubjectLifeCycle() != null && omsAlbumFileRelation.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAlbumFileRelation.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAlbumFileRelation);
    }

    /**
     * 8-02-02删除专辑文件关系[10314]
     * gen by moon at 11/11/2024, 1:25:12 AM
     */
    @Trace(operationName = "8-02-02删除专辑文件关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAlbumFileRelation(String albumRelationId) {

        return super.removeById(albumRelationId);
    }

    /**
     * 8-02-02查询专辑文件关系列表[10315]
     * gen by moon at 11/17/2024, 6:31:38 PM
     */
    @Trace(operationName = "8-02-02查询专辑文件关系列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAlbumFileRelation> queryAlbumFileRelationList(QueryAlbumFileRelationListReq reqDto) {

        try {
            QueryWrapper<OmsAlbumFileRelation> queryWrapper = new QueryWrapper<>();
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAlbumFileRelation.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAlbumFileRelation.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAlbumFileRelation.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAlbumFileRelation>) super.list(queryWrapper)).stream().map(item -> {
                item.setResourceFileRelaPath(CommonFunctionHelper.getFilePath(item.getResourceFileRelaPath()));//otherModel: ALL_PATH
                return item;
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 8-02-02查询专辑文件关系详情[10341]
     * gen by moon at 11/11/2024, 1:25:18 AM
     */
    @Trace(operationName = "8-02-02查询专辑文件关系详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAlbumFileRelation queryAlbumFileRelationDetail(QueryAlbumFileRelationDetailReq reqDto) {

        try {
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<OmsAlbumFileRelation> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAlbumFileRelation> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAlbumFileRelation retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 8-02-04批量增专辑文件关系[10368]
     * gen by moon at 11/21/2024, 9:37:55 PM
     */
    @Trace(operationName = "8-02-04批量增专辑文件关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddAlbumFileRelation(List<OmsAlbumFileRelation> omsAlbumFileRelation) {

        if (omsAlbumFileRelation == null || omsAlbumFileRelation.size() == 0) {
            return false;
        }

        return super.saveBatch(omsAlbumFileRelation.stream().map(item -> {
            item.setAlbumRelationId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setIsArchive("FALSE");
            item.setSubjectLifeCycle("CURRENTLY_VALID");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
            }
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("ARCHIVING")) {
                item.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
            }
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
                item.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
            if (item != null && item.getSubjectLifeCycle() != null && item.getSubjectLifeCycle().equals("ARCHIVING")) {
                item.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
            }
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 8-02-02批量删专辑文件关系[10537]
     * gen by moon at 5/27/2025, 1:20:41 PM
     */
    @Trace(operationName = "8-02-02批量删专辑文件关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteAlbumFileRelation(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }
}
